<?php

namespace App\Services\Product;

use App\Exceptions\DeviceException;
use App\Exceptions\SystemException;
use App\Exceptions\UserException;
use App\Facades\DeviceManager;
use App\Facades\UserManager;
use Exception;

class DeviceInfoService
{
	/**
	 * 添加安装信息
	 * @param  $data  安装信息数组
	 */
	public function addInstallInfo($data)
	{
		$installInfo = DeviceManager::getInstallInfoByQueryColumn('device_code', $data['device_code']);
		if($installInfo){
			throw new DeviceException('installInfo.exists');
		}
		DeviceManager::addInstallInfo($data);
	}

	/**
	 * 更新安装信息
	 * @param   $id   	记录 id
	 * @param   $data   安装信息数组
	 * @return        
	 */
	public function updateInstallInfo($id, $data)
	{
		$installInfo = DeviceManager::getInstallInfoByQueryColumn('id', $id);
		try{
			$installInfo->update($data);
		}catch(Exception $e){
			dd($e->getMessage());
			throw new SystemException('database');
		}
	}

	/**
	 * 添加维护信息
	 * @param  $data 维护信息数组
	 */
	public function addMaintainInfo($data)
	{
		DeviceManager::addMaintainInfo($data);
	}

	/**
	 * 更新维护数据
	 * @param   $id   
	 * @param   $data  维护信息数组
	 * @return        
	 */
	public function updateMaintainInfo($id, $data)
	{
		$installInfo = DeviceManager::getMaintainInfoByQueryColumn('id', $id);
		try{
			$installInfo->update($data);
		}catch(Exception $e){
			throw new SystemException('database');
		}

	}

	/**
	 * 获取安装信息列表
	 * @param   $condition 	搜索条件。可空
	 * @return              
	 */
	public function findInstallInfoList($condition)
	{
		$installInfoBuilder = DeviceManager::getInstallInfoListBuilderOrderByCreatedAt();
		if(!empty($condition)){
            $installInfoBuilder->where('device_code', 'like', '%'.$condition.'%')->orWhere('dealer', 'like', '%'.$condition.'%')->orWhere('customer_name', 'like', '%'.$condition.'%');
        }
		return $installInfoBuilder->latest()->paginate(15);
	}

	/**
	 * 获取维护信息列表
	 * @param   $condition 设备码，可空
	 * @return              
	 */
	public function findMaintainInfoList($condition)
	{
		$maintainInfoBuilder = DeviceManager::getMaintainInfoListBuilderOrderBy('status');
		if(!empty($condition)){
		    $maintainInfoBuilder->where('device_code', 'like', '%'.$condition.'%')->orWhere('customer_name', 'like', '%'.$condition.'%');
		}
		return $maintainInfoBuilder->orderBy('reserve_at')->orderBy('maintain_at')->paginate(15);
	}

	/**
	 * 获取维护信息实例
	 * @param   $id 记录 id
	 * @return      
	 */
	public function getMaintainInfoById($id)
	{
		return DeviceManager::getMaintainInfoByQueryColumn('id', $id);
	}

	/**
	 * 删除维护信息
	 * @param   $id  记录 id
	 * @return      
	 */
	public function deleteMaintainInfo($id)
	{
		$maintainInfo = DeviceManager::getMaintainInfoByQueryColumn('id', $id);
		$maintainInfo->delete();
	}

	/**
	 * 删除安装信息
	 * @param   $id   记录 id
	 * @return      
	 */
	public function deleteInstallInfo($id)
	{
		$installInfo = DeviceManager::getInstallInfoByQueryColumn('id', $id);
		$installInfo->delete();
	}

	/**
	 * 获取安装信息实例
	 * @param   $deviceCode  设备码
	 * @return              
	 */
	public function getInstallInfoByDeviceCode($deviceCode)
	{
		$installInfo = DeviceManager::getInstallInfoByQueryColumn('device_code', $deviceCode);
		return $installInfo;
	}

	public function findmaintainInfoByCustomerName($customerName)
	{
		$maintainBuilder = DeviceManager::findmaintainInfoBuilderByColumn('customer_name', $customerName);
		return $maintainBuilder->orderBy('reserve_at', 'desc')->orderBy('maintain_at', 'desc')->paginate(15);
	}


}